<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2020/10/16
 * Time: 10:56
 */


namespace app\adminapiv3\service;


use app\common\service\asset_manage\address\HostAddress;
use app\common\service\asset_manage\host\Host;
use app\common\service\asset_manage\ministry\HostManager;
use app\common\service\asset_manage\ministry\HostMinistry;
use app\common\service\asset_manage\software\HostSoftware;
use app\common\service\Host as CommonHost;
use app\common\service\HostModuleRelation;
use app\common\service\HostTag;

use app\common\service\platform_manage\ServerAuthService;
use app\common\service\module_manage\netfilter\HostLearnConfig;

class CommonHostService extends BaseService
{
    private $remotePath = '/tmp/Install.sh';//远程安装路径
    protected $serviceId_a = [];//服务商id
    protected $softwareId_a = [];//所属业务系统
    protected $managerId_a = [];//安全管理员
    protected $addressId_a = [];//资产所属位置
    protected $ministryId_a = [];//所属部门

    public function __construct()
    {
        $HostModuleRelation = new HostModuleRelation();
        $HostModuleRelation->fieldCheck($this->module_name);
    }
    /**
     * @name: 列表
     * @Param:
     * @Description:
     * @Author: foo
     * @Date: 2019-10-24 16:10:07
     */
    public function list ($data, $isIncludeGoupIdNull = false)
    {
        $CommonHost = new CommonHost();
        $return = $CommonHost->list($data, $this->module_name, $isIncludeGoupIdNull);

        $ServerAuthService = new ServerAuthService();
        $return['remianing_number'] = $ServerAuthService->remainingNum($this->module_name);
        !isset($return['data']) ? : $return['data'] = $this->processList($return['data']);

        return $return;
    }

    /**
     * @name: 列表处理
     * @Param:
     * @Description:
     * @Author: foo
     * @Date: 2019-10-24 16:23:11
     */
    public function processList ($data)
    {
        if (empty($data))
        {
            return [];
        }
        $return = [];
        foreach ($data as  $k => $v)
        {

            $hostId_a[] = $v['id'];
            $this->addressId_a[] = $v['address_id'];
            $this->ministryId_a[] = $v['ministry_id'];
            $this->managerId_a[] = $v['manager_id'];
            $this->softwareId_a[] = $v['software_id'];
            $this->serviceId_a[] = $v['safe_provider_id'];
            $this->serviceId_a[] = $v['develop_provider_id'];
            $this->serviceId_a[] = $v['maintain_provider_id'];
        }

        $extraData = $this->extraData();
        $moduleData = $this->moduleData($this->module_name, $hostId_a);
        foreach ($data as $k => $v)
        {
            $return[$k] = $this->processOne($extraData, $v);
            !isset($moduleData[$v['id']]) ? : $return[$k][$this->module_name] = $moduleData[$v['id']];
        }

        return $return;
    }

    /**
     * @name: 模块数据
     * @Param:
     * @Description:
     * @Author: foo
     * @Date: 2020-01-06 13:04:17
     */
    public function moduleData ($hostId_a)
    {
        if ($this->module_name == 'client')
        {
            return [];
        }

        $return = $this->config($hostId_a);

        return $return;
    }

    /**
     * @name: 额外的附属数据
     * @Param:
     * @Description:
     * @Author: foo
     * @Date: 2019-10-25 13:38:12
     */
    public function extraData ()
    {
        $hostTagServer = new HostTag();
        $return['tag'] = $hostTagServer->showTag([], 'id,name,icon', 'id');

        $hostSoftwareServer = new HostSoftware();
        $return['software'] = $hostSoftwareServer->showSoftware($this->softwareId_a, 'id,name,status', 'id');

        return $return;
    }


    /**
     * @name: 单个处理
     * @Param:
     * @Description:
     * @Author: foo
     * @Date: 2019-10-25 13:34:18
     */
    public function processOne ($extraData, $data)
    {
        $params = [
            'id',//客户端id
            'name',//客户端名
            'alias',//客户端别名
            'ip',//客户端ip
            'system_name',//系统名
            'os',//系统类别
            'online',//是否在线
            'version',//客户端/模块 版本号
            'hostkey',//资产绑定码
        ];
        foreach ($params as $v)
        {
            isset($data[$v]) ? $return[$v] = $data[$v] : '';
        }
        !isset($extraData['tag']) ? : $return['tag'] = $this->processTag($extraData['tag'], $data['tag']);
        !isset($extraData['software']) ? : $return['software'] = $this->processSoftware($extraData['software'], $data['software_id']);//安全责任人
        !isset($extraData['ministry']) ? : $return['ministry'] = $this->processMinistry($extraData['ministry'], $data['ministry_id']);//安全责任人
        !isset($extraData['address']) ? : $return['address'] = $this->processAddress($extraData['address'], $data['address_id']);//安全责任人
        !isset($extraData['manager']) ? : $return['manager'] = $this->processSafeManager($extraData['manager'], $data['manager_id']);//安全责任人
        !isset($extraData['develop_provider']) ? : $return['develop_provider'] = $this->processService($extraData['service'], $data['develop_provider_id']);//开发商
        !isset($extraData['maintain_provider']) ? : $return['maintain_provider'] = $this->processService($extraData['service'], $data['maintain_provider_id']);//开发商
        !isset($extraData['safe_provider']) ? : $return['safe_provider'] = $this->processService($extraData['service'], $data['safe_provider_id']);//开发商

        return $return;
    }

    /**
     * @name: 添加标签
     * @Param:
     * @Description:
     * @Author: foo
     * @Date: 2019-10-25 13:44:49
     */
    public function processTag($tagData, $hostTag)
    {
        $return = [];
        if (empty($hostTag))
        {
            return $return;
        }

        $tag_a = explode(',',$hostTag);
        foreach ($tag_a as $tag_id)
        {
            if(isset($tagData[$tag_id]))
            {
                $return[] = [
                    'id' => $tagData[$tag_id]['id'],
                    'name' => $tagData[$tag_id]['name'],
                    'icon' => $tagData[$tag_id]['icon'],
                ];
            }
        }

        return $return;
    }

    /**
     * @name: 安全关管理员
     * @Param:
     * @Description:
     * @Author: foo
     * @Date: 2019-10-25 14:05:11
     */
    public function processSafeManager ($safeManagerData, $hostSafeManagerId)
    {
        $return = !isset($safeManagerData[$hostSafeManagerId]) ? [] : $safeManagerData[$hostSafeManagerId];

        return $return;
    }
    /**
     * @name: 地址
     * @Param:
     * @Description:
     * @Author: foo
     * @Date: 2019-10-25 14:05:11
     */
    public function processAddress ($addressData, $hostAddressId)
    {
        $return = !isset($addressData[$hostAddressId]) ? [] : $addressData[$hostAddressId];

        return $return;
    }
    /**
     * @name: 所属系统
     * @Param:
     * @Description:
     * @Author: foo
     * @Date: 2019-10-25 14:05:11
     */
    public function processSoftware ($softwareData, $hostSoftwareId)
    {
        $return = !isset($softwareData[$hostSoftwareId]) ? [] : $softwareData[$hostSoftwareId];

        return $return;
    }
    /**
     * @name: 部门
     * @Param:
     * @Description:
     * @Author: foo
     * @Date: 2019-10-25 14:05:11
     */
    public function processMinistry ($ministryData, $hostMinistryId)
    {
        $return = !isset($ministryData[$hostMinistryId]) ? [] : $ministryData[$hostMinistryId];

        return $return;
    }

    /**
     * @name: 处理服务商
     * @Param:
     * @Description:
     * @Author: foo
     * @Date: 2019-10-25 13:59:21
     */
    public function processService ($serviceData, $hostServiceId)
    {
        if (!isset($serviceData[$hostServiceId]))
        {
            return [];
        }

        $msg = '(合作已终止)';
        if($serviceData[$hostServiceId]['status'] == 2)
        {
            $serviceData[$hostServiceId]['name'] .= $msg;
            $serviceData[$hostServiceId]['contact'] .= $msg;
        }

        return $serviceData[$hostServiceId];
    }


    /**
     * @name: 筛选数据
     * @Param:
     * @Description:
     * @Author: foo
     * @Date: 2019-10-30 10:41:16
     */
    public function filterData ()
    {

        $HostAddress = new HostAddress();
        $return['address'] = $HostAddress->showAddress([], 'id,name');

        $HostTag = new HostTag();
        $return['tag'] = $HostTag->toDo('showTag', [[], 'id,name,icon']);

        $HostSoftWare = new HostSoftware();
        $return['software'] = $HostSoftWare->showSoftware([], 'id,name');

        $HostManager = new HostManager();
        $return['manager'] = $HostManager->showManager([], 'id,name');


        $HostService = new \app\common\service\asset_manage\service_provider\HostService();

        $nochange = [['id'=> 0, 'name'=>'请选择', 'contact'=> '请选择']];

        $return['develop_provider'] = array_merge($nochange, $HostService->extraService(1));
        $return['safe_provider'] = array_merge($nochange, $HostService->extraService(3));
        $return['maintain_provider'] = array_merge($nochange, $HostService->extraService(2));
        $return['software'] = array_merge($nochange, $return['software']);

        return $return;
    }

}